# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Jelle van der Waa <jelle@archlinux.org>
# Contributor: Stéphane Gaudreault <stephane@archlinux.org>

pkgname=cppcheck
pkgver=2.10.3
pkgrel=2
pkgdesc='A tool for static C/C++ code analysis'
arch=('loong64' 'x86_64')
url='http://cppcheck.sourceforge.net/'
license=('GPL')
depends=(
  'python-pygments'
  'tinyxml2'
  'pcre'
)
makedepends=(
  'git'
  'docbook-xsl'
  'qt5-tools'
  'qt5-base'
  'python'
  'pandoc'
  'cmake'
  'clang'
)
optdepends=(
  'qt5-tools: run cppcheck-gui'
  'python-pygments: cppcheck-htmlreport'
  'clang: for cppcheck-gui integration'
)
_commit='59030ef53dff61885c058583d2aef94f970ffdc8'
source=(
  "$pkgname::git+https://github.com/danmar/cppcheck.git#commit=$_commit"
  'translations-location.patch'
)
sha512sums=('SKIP'
            'd3528834d719017ec3a0e08005a293089b556622928defa1b37f940e62cb01165dcbd741e6d5e989c0156fb8789f7e63702af8b2390738648b2300a92f4ab0ae')
b2sums=('SKIP'
        '8156920eacc630cb5eceb2387937b747c84c6325bef906717cfbad68c122bdd27965da1e8070a560a0bed3a7b7c59ff5f0e116bb1d035c4c42f430c927a75b1f')

pkgver() {
  cd "$pkgname"

  git describe --tags
}

prepare() {
  cd "$pkgname"

  # fix location of translations
  patch -p1 -i ../translations-location.patch
}

build() {
  export CXXFLAGS+=" -DNDEBUG"

  # Manuals
  pushd "$pkgname"
  xsversion=$(pacman -Qi docbook-xsl | grep ^Version | sed -e 's/.*: //' -e 's/-[0-9]$//')
  make DB2MAN=/usr/share/xml/docbook/xsl-stylesheets-${xsversion}-nons/manpages/docbook.xsl man
  pandoc man/manual.md -o man/manual.html -s --number-sections --toc
  pandoc man/reference-cfg-format.md -o man/reference-cfg-format.html -s --number-sections --toc
  popd

  cmake \
    -B build \
    -S "$pkgname" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DUSE_MATCHCOMPILER=ON \
    -DHAVE_RULES=ON \
    -DBUILD_GUI=ON \
    -DBUILD_SHARED_LIBS=OFF \
    -DBUILD_TESTS=ON \
    -DFILESDIR=/usr/share/cppcheck \
    -DUSE_BUNDLED_TINYXML2=OFF

  cmake --build build
}

check() {
  cd build/bin
  ./testrunner -g -q
}

package() {
  DESTDIR="$pkgdir" cmake --install build

  cd "$pkgname"

  install -vDm644 -t "$pkgdir/usr/share/man/man1" cppcheck.1
  install -vDm755 -t "$pkgdir/usr/bin" htmlreport/cppcheck-htmlreport

  # Fix location of language files
  install -d "${pkgdir}"/usr/share/cppcheck/cfg/lang
  mv "${pkgdir}"/usr/bin/cppcheck_*.qm "${pkgdir}"/usr/share/cppcheck/cfg/lang/
}
